package com.yoomiito.app.ui.list

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import android.text.TextPaint
import android.widget.LinearLayout
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.yoomiito.app.MainActivity
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.sdd.SddFreeGoodsOrderInfo
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.showToast
import com.yoomiito.app.utils.*
import com.yoomiito.app.widget.SpacingDecoration
import com.yoomiito.app.widget.dialog.SendGoodsDialog
import com.yoomiito.app.widget.songduoduo.GiverListView
import com.yoomiito.app.widget.state.ContentType
import com.yoomiito.app.widget.state.EmptyType
import com.yoomiito.app.widget.state.StatusManager
import kotlinx.android.synthetic.main.act_goods_sort.*
import kotlinx.android.synthetic.main.fm_rcy.*
import kotlinx.android.synthetic.main.title_bar.*
import org.greenrobot.eventbus.EventBus

/**
 * 送多多--申请者列表
 */
class ReceiveListOfGiftActivity :BaseActivity<ReceiveListOfGiftPresenter>(){
    companion object{
        fun start(context: Context,type:Int){
            val intent = Intent(context,ReceiveListOfGiftActivity::class.java)
            intent.putExtra(Const.Common.Type,type)
            context.startActivity(intent)
        }
    }
    private var pageType:Int = 0
    private var mCurrentPage = 1
    private val mAdapter = ReceiverListAdapter(null)
    override fun getLayoutId(): Int = R.layout.title_framelayout

    override fun newP(): ReceiveListOfGiftPresenter = ReceiveListOfGiftPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        pageType = intent.getIntExtra(Const.Common.Type,0)
        iv_back_left.setOnClickListener { finish() }
        tv_center.text = if (pageType==GiverListView.type_0) "待确认" else "其他"
        tv_center.paint.isFakeBoldText = true
        mStatusManager = StatusManager.Builder(this)
                .addType(ContentType(R.layout.fm_rcy))
                .addType(EmptyType().setEmptyContent("暂无更多"))
                .build()
        frameLayout.addView(mStatusManager.view())

        initView()
        showLoading()
        requestData()
    }

    private fun requestData(){
        p.sddFreeGoodsOrderLIst(mCurrentPage,pageType)
    }

    private fun initView() {
        parentView.setBackgroundResource(R.color.color_gray_F8F8F8)
        fm_refreshLayout.setOnRefreshListener {
            mCurrentPage=1
            requestData()
        }

        fm_refreshLayout.setOnLoadMoreListener {
            mCurrentPage++
            requestData()
        }

        fm_rcy.setBackgroundResource(R.color.color_white)
        val lp = fm_rcy.layoutParams as SmartRefreshLayout.LayoutParams
        val d = DensityUtil.dp2px(12f)
        lp.setMargins(d,d,d,d)
        fm_rcy.layoutManager = androidx.recyclerview.widget.GridLayoutManager(this, 4)
        fm_rcy.addItemDecoration(SpacingDecoration(DensityUtil.dp2px(22f),DensityUtil.dp2px(22f),true))
        fm_rcy.adapter = mAdapter
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            showDialog(mAdapter.data[position])
        }
    }

    fun data(data:BaseList<SddFreeGoodsOrderInfo>?){
        dismissLoading()
        if (data?.current_page==1){
            fm_refreshLayout.finishRefresh()
            if (data?.data==null||data?.data.isEmpty()){
                mStatusManager.showEmpty()
            }else{
                mAdapter.setNewData(data?.data)
                mStatusManager.showContent()
            }
        }else{
            fm_refreshLayout.finishLoadMore()
            mAdapter.addData(data?.data!!)
        }
        if (data?.current_page==data?.last_page){
            fm_refreshLayout.finishLoadMoreWithNoMoreData()
        }
        mCurrentPage = data?.current_page!!
    }

    private fun showDialog(item:SddFreeGoodsOrderInfo){
        val dialog = SendGoodsDialog(context,item)
        dialog.onResultListener={b->
            reviewStatus(item.id.toString(),if (b)0 else 1)
        }
        dialog.show()
    }

    private fun reviewStatus(id:String,status:Int){
        YmApi.getYmService()
                .sddConfirmGift(id,status)
                .compose(bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<BaseModel<*>>(){
                    override fun onSuccess(t: BaseModel<*>?) {
                        val msg = EventMessage()
                        msg.eventType = Type.EventMsgType.SDD_UPDATE_DATA
                        EventBus.getDefault().post(msg)
                        dismissLoading()
                    }

                    override fun onFail(error: ApiException?) {
                        error?.msg?.showToast()
                        dismissLoading()
                    }
                })
    }

}